#pragma once

#include <gtl/thread/locker.h>

namespace gtl
{

class mutex
{
	mutex(const mutex&);
	mutex& operator=(const mutex&);

public:
	mutex(const tchar* name = NULL, bool create = true)
	{
		m_mutex = NULL;
		if(create)
			this->create(name);
	}

	~mutex()
	{
		unlock();
		CloseHandle(m_mutex);
	}

public:
	void lock(uint32 time = INFINITE)
	{
		WaitForSingleObject(m_mutex, time);
	}

	void unlock()
	{
		ReleaseMutex(m_mutex);
	}

public:
	void create(const tchar* name)
	{
		m_mutex = CreateMutex(NULL, FALSE, name);
	}

protected:
	HANDLE m_mutex;
};


} // end of namespace gtl